package com.situ.mvc.service.impl;

import java.util.List;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.dao.IUserDao;
import com.situ.mvc.dao.impl.UserDaoImpl;
import com.situ.mvc.entity.User;
import com.situ.mvc.service.IUserService;

public class UserServiceImpl implements IUserService {
	private IUserDao userDao = new UserDaoImpl();

	@Override
	public ServerResponse login(String name, String password) {
		//if (userDao.checkName(name) == false) {
		if ("lisi".equals(name)) {
			return ServerResponse.createError("用户名不存在");
		}
		
		User user = userDao.login(name, password);
		if (user == null) {
			return ServerResponse.createError("密码错误");
		}
		return ServerResponse.createSuccess("登录成功", user);
	}

	@Override
	public ServerResponse pageList(Integer page, Integer limit) {
		//1、ServerResponse里面的count，代表员工总数量
		int count = userDao.findTotalCount();
		//2、ServerResponse里面的data，代表但前页的数据
		int offset = (page - 1) * limit;
		List<User> data = userDao.pageList(offset, limit);
		
		return ServerResponse.createSuccess("查找成功", count, data);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = userDao.deleteById(id);
		if (count == 1) {
			return ServerResponse.createSuccess("删除成功");
		}
		
		return ServerResponse.createError("删除失败");
	}

	@Override
	public ServerResponse add(User user) {
		int count = userDao.add(user);
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		}
		
		return ServerResponse.createError("添加失败");
	}

	@Override
	public User findById(Integer id) {
		return userDao.findById(id);
	}

	@Override
	public ServerResponse update(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		// "1,2,3"  ["1","2","3"]
		String[] array = ids.split(",");
		for (String id : array) {
			userDao.deleteById(Integer.parseInt(id));
		}
		// delete id from user where id in(1,2,3);
		return ServerResponse.createSuccess("删除成功");
	}

}
